﻿using MobaCommon.Dto;
using MOBAServer.Model;
using System;
using System.Collections.Generic;
using System.Text;

namespace MOBAServer.Room
{
    public class SelectRoom : RoomBase<MobaClient>
    {
        /// <summary>
        /// 队伍1玩家ID对应的选择模型
        /// </summary>
        public Dictionary<int, SelectModel> team1Dic;
        /// <summary>
        /// 队伍2玩家ID对应的选择模型
        /// </summary>
        public Dictionary<int, SelectModel> team2Dic;
        /// <summary>
        /// 进入的数量
        /// </summary>
        public int enterCount;

        public bool IsAllEnter {
            get {
                return enterCount >= roomCount;
            }
        }
        /// <summary>
        /// 准备的数量
        /// </summary>
        public int readyCount;

        public bool IsAllReady {
            get {
                return roomCount==clientList.Count&&readyCount == enterCount;
            }
        }
        public SelectRoom(int id, int count) : base(id, count)
        {
            team1Dic = new Dictionary<int, SelectModel>();
            team2Dic = new Dictionary<int, SelectModel>();
            enterCount = 0;
            readyCount = 0;
        }
        /// <summary>
        /// 初始化房间
        /// </summary>
        /// <param name="team1"></param>
        /// <param name="team2"></param>
        public void InitRoom(List<int> team1,List<int> team2)
        {
            PlayerModel playerModel;
            SelectModel model;
            foreach (var item in team1)
            {
                playerModel = Cache.Caches.Player.GetModel(item);
                model = new SelectModel(item,playerModel.Name);
                team1Dic.Add(playerModel.Id,model);
            }
            foreach (var item in team2)
            {
                playerModel = Cache.Caches.Player.GetModel(item);
                model = new SelectModel(item, playerModel.Name);
                team2Dic.Add(playerModel.Id, model);
            }
        }
        /// <summary>
        /// 进入房间
        /// </summary>
        public void Enter(int playerId,MobaClient client)
        {
            if (team1Dic.ContainsKey(playerId))
            {
                team1Dic[playerId].isEnter = true;
            }
            else if (team2Dic.ContainsKey(playerId))
            {
                team2Dic[playerId].isEnter = true;
            }
            else
                return;
            //添加房间内的连接对象
            clientList.Add(client);
            enterCount++;
        }
        /// <summary>
        /// 选择英雄
        /// </summary>
        /// <param name="playerId"></param>
        /// <param name="heroId"></param>
        public bool Select(int playerId,int heroId)
        {
            //队友有没有选择
            if (team1Dic.ContainsKey(playerId))
            {
                foreach (var item in team1Dic.Values)
                {
                    if (item.heroId == heroId)
                        return false;
                }
                team1Dic[playerId].heroId = heroId;

            }
            else if(team2Dic.ContainsKey(playerId))
            {
                foreach (var item in team2Dic.Values)
                {
                    if (item.heroId == heroId)
                        return false;
                }
                team2Dic[playerId].heroId = heroId;
            }
            return true;
        }
        /// <summary>
        /// 确认选择
        /// </summary>
        /// <param name="playerId"></param>
        /// <returns></returns>
        public bool Ready(int playerId)
        {
            if(team1Dic.ContainsKey(playerId))
            {
                SelectModel model = team1Dic[playerId];
                if (model.heroId == -1)
                    return false;
                model.isReady = true;
                readyCount++;
                
            }
            else
            {
                SelectModel model = team2Dic[playerId];
                if (model.heroId == -1)
                    return false;
                model.isReady = true;
                readyCount++;
            }
            return true;
        }
    }
}
